package com.gitee.wsl.markdown.plugin.gfm

import com.gitee.wsl.markdown.Markwon
import com.gitee.wsl.markdown.plugin.createMarkwonPlugin
import org.intellij.markdown.MarkdownTokenTypes
import org.intellij.markdown.flavours.gfm.GFMFlavourDescriptor
import org.intellij.markdown.flavours.gfm.GFMTokenTypes
import org.intellij.markdown.flavours.gfm.StrikeThroughDelimiterParser
import org.intellij.markdown.flavours.gfm.table.GitHubTableMarkerBlock
import org.intellij.markdown.flavours.gfm.table.GitHubTableMarkerProvider
import org.intellij.markdown.parser.sequentialparsers.EmphasisLikeParser
import org.intellij.markdown.parser.sequentialparsers.impl.AutolinkParser
import org.intellij.markdown.parser.sequentialparsers.impl.EmphStrongDelimiterParser
import org.intellij.markdown.parser.sequentialparsers.impl.MathParser

/*class GFMPlugin(useSafeLinks: Boolean = true,
                absolutizeAnchorLinks: Boolean = false,
                makeHttpsAutoLinks: Boolean = false
) : AbstractMarkwonPlugin() {

    override val key: AttributeKey<MarkwonPlugin> = AttributeKey("GFM")

    val GFMFlaver by lazy { GFMFlavourDescriptor(useSafeLinks, absolutizeAnchorLinks, makeHttpsAutoLinks) }

    override fun prepare(block: MarkwonConfiguration.() -> Unit) = this

    override fun configureParser(builder: MutableMarkdownFlavourDescriptor) {
        super.configureParser(builder)

        builder.registerSequentialParser(
            AutolinkParser(listOf(MarkdownTokenTypes.AUTOLINK, GFMTokenTypes.GFM_AUTOLINK)),
            MathParser(),
            EmphasisLikeParser(EmphStrongDelimiterParser(), StrikeThroughDelimiterParser())
        )

        builder.registerMarkerBlockProvider(
            GitHubTableMarkerBlock::class to GitHubTableMarkerProvider()
        )

        builder.registerHtmlGeneratingProvider { linkMap, baseURI ->
            GFMFlaver.createHtmlGeneratingProviders(linkMap, baseURI)
        }

    }

}*/

val Markwon.Companion.PLUGIN_GMF get() = "GMF"

fun Markwon.useGFM(useSafeLinks: Boolean = true,
                   absolutizeAnchorLinks: Boolean = false,
                   makeHttpsAutoLinks: Boolean = false): Markwon {
    val gmfPlugin = createMarkwonPlugin(Markwon.PLUGIN_GMF) {

        val GFMFlaver by lazy { GFMFlavourDescriptor(useSafeLinks, absolutizeAnchorLinks, makeHttpsAutoLinks) }

        this.configureParser {
            this.inlinesLexer = GFMFlaver.createInlinesLexer()

            registerSequentialParser(
                AutolinkParser(listOf(MarkdownTokenTypes.AUTOLINK, GFMTokenTypes.GFM_AUTOLINK)),
                MathParser(),
                EmphasisLikeParser(EmphStrongDelimiterParser(), StrikeThroughDelimiterParser())
            )

            registerMarkerBlockProvider(
                GitHubTableMarkerBlock::class to GitHubTableMarkerProvider()
            )

            registerHtmlGeneratingProvider { linkMap, baseURI ->
                GFMFlaver.createHtmlGeneratingProviders(linkMap, baseURI)
            }
        }

    }
    config.install(gmfPlugin)
    return this
}